<?php

define ("CM_ALLOWED_SCHEMA_EXTENSIONS", 'xsd dtd rng');
define ("CM_SCHEMA_DIR", 'sites/all/modules/cm_content/');

/**
 * Implements hook_settings_form().
 *
 */
function cm_settings_form($form, &$form_state) {
  
  $form = array();

  $form['explanation'] = array(
    '#markup' => t('<h2>CodeMirror Settings</h2><br><br>'),

  );
  
  // to show line numbers to the left of the editor
  $form['cm_lang'] = array(
	'#type' => 'select',
	'#title' => t ('Select Language'),
	'#description' => t('Select the appropriate language'),
	'#options' => array(
	  'HTML' => 'HTML',
	  'XML' => 'XML',
	  'TEI' => 'TEI',
	  'CSS' => 'CSS',
	),
	'#attributes' => array(
	  'style' => 'min-width: 100px;'
	),
	'#default_value' => variable_get('cm_lang', t('HTML')),
  );
	  
  $form['line_numbers'] = array(
    '#type' => 'checkbox',
    '#title' => t ('Line Numbers'),
	'#default_value' => variable_get('line_numbers', FALSE),
    '#description' => t('Enable Line Numbers in the textarea'),
  );
  
  $form['auto_tag_closing'] = array(
    '#type' => 'checkbox',
    '#title' => t ('Auto Tag Closing'),
	'#default_value' => variable_get('auto_tag_closing', FALSE),
    '#description' => t('Enable Autotag Closing'),
  );
   
  $form['auto_completion'] = array(
    '#type' => 'checkbox',
    '#title' => t ('Auto Completion for XML Tags'),
	'#default_value' => variable_get('auto_completion', FALSE),
    '#description' => t('Enable Auto Completion for XML Tags'),
  );
  
  $form['cm_fontsize'] = array(
    '#type' => 'select',
    '#title' => t('Font size'),
    '#options' => array(
      '9pt' => '9pt',
      '10pt' => '10pt',
      '11pt' => '11pt',
      '12pt' => '12pt',
    ),
    '#default_value' => variable_get('cm_fontsize', '11pt'),
    '#attributes' => array(
      'style' => 'min-width: 100px;'
      ),
    );


  return system_settings_form($form);

}

function cm_schema_upload_form($form, &$form_state) {
  
  // Validator only xsd, dtd, rng
  $validators = array(
     'file_validate_extensions' => array( 0 => CM_ALLOWED_SCHEMA_EXTENSIONS)
  );
  
  $form = array();
  
  $form['explanation'] = array(
    '#markup' => t('<h2>Schema Upload</h2><br><br>'),

  );
  
  $form['schema_upload'] = array(
	'#type' => 'managed_file',
	'#title' => t('Choose a Schema to upload'),
	'#upload_location' =>  CM_SCHEMA_DIR,
	'#progress_message' => t('Uploading schema file. Please wait...'),
	'#extended' => TRUE,
	'#upload_validators' => $validators
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Schema'),
  );
  
  return $form;
}

/**
 * hook_validate() checks if user supplied a valid file
 */
function cm_schema_upload_form_validate($form, &$form_state) {
  if (empty($form_state['values']['schema_upload']['fid'])) {
    form_set_error('schema_upload', t('Please select a schema to upload.'));
  }
}

/**
 * Checks submitted schema file
 * saves it and marks it as 'permanent' if valid
 *
 */
function cm_schema_upload_form_submit($form, &$form_state) {

  //  save font if valid file fid != 0
  if ($form_state['values']['schema_upload']['fid'] != 0) {
    $file = file_load($form_state['values']['schema_upload']['fid']);
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);
	
    variable_set('schema_upload', $file->fid);
    drupal_set_message(t('Your schema (@schema_name, id:@schema_id) has been uploaded and saved successfully', array('@schema_name' => $file->filename, '@schema_id' => $file->fid)));
  }
  // file removed
  elseif ($form_state['values']['schema_upload']['fid'] == 0) {
    $fid = variable_get('schema_upload', FALSE);
    $file = $fid ? file_load($fid) : FALSE;
    if ($file) {
      file_delete($file);
    }
    drupal_set_message(t('The schema file @schema_file was removed', array('@schema_file' => $file->filename)));
  }

  variable_set('schema_upload', FALSE);

}

